package course.view;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import course.controller.CourseControllerListener;
import course.po.Course;

public class CourseDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private CourseControllerListener courseControllerListener;
	private JButton confirmButton;
	private JPanel courseAddPanel;
	private Course course;
	private JTextField courseIdTextField;
	private JTextField courseTypeTextField;
	private JTextField courseNoTextField;
	private JTextField courseNameTextField;
	private JTextField courseDetailTextField;
	private JTextField courseCreditTextField;
	private JTextField teacherIdTextField;
	private JTextField teacherNameTextField;
	private JTextField maxNumberTextField;
	private JTextField selectNumberTextField;

	public CourseDialog(CourseJPanel owner, String title, Course course) {
		this.courseControllerListener = owner.getControllerListener(); // 让contactsFrame和contactsAddDialog共用同一个controller对象
		this.courseControllerListener.setCourseAddDialog(this);
		this.setTitle(title);
		this.course = course;
		this.add(getCourseAddPanel());
		this.setModal(true); // 设有模
		this.setSize(370, 530);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
		this.validate();
	}

	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	/**
	 * 添加/修改课程表单面板
	 * 
	 * @return
	 */
	private JPanel getCourseAddPanel() {
		courseAddPanel = new JPanel();
		courseAddPanel.setBackground(Color.orange);
		if ("添加课程".equals(this.getTitle())) {
			courseIdTextField = new JTextField(30);
			courseTypeTextField = new JTextField(30);
			courseNoTextField = new JTextField(30);
			courseNameTextField = new JTextField(30);
			courseDetailTextField = new JTextField(30);
			courseCreditTextField = new JTextField(30);
			teacherIdTextField = new JTextField(30);
			maxNumberTextField = new JTextField(30);
			selectNumberTextField = new JTextField(30);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addPost"); // 添加课程
		} else {
			courseIdTextField = new JTextField(String.valueOf(course.getCouId()), 30);
			courseTypeTextField = new JTextField(course.getCouType(), 30);
			courseNoTextField = new JTextField(course.getCouName(), 30);
			courseNameTextField = new JTextField(course.getCouName(), 30);
			courseDetailTextField = new JTextField(course.getCouDetail(), 30);
			courseCreditTextField = new JTextField(String.valueOf(course.getCouCredit()), 30);
			teacherIdTextField = new JTextField(String.valueOf(course.getTeacher().getTeaId()), 30);
			maxNumberTextField = new JTextField(String.valueOf(course.getMaxNumber()), 30);
			selectNumberTextField = new JTextField(String.valueOf(course.getSelectNumber()), 30);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updatePost"); // 修改课程
		}
		confirmButton.addActionListener(courseControllerListener);
		courseAddPanel.add(new JLabel("编号"));
		courseAddPanel.add(courseIdTextField);
		courseAddPanel.add(new JLabel("课程类型"));
		courseAddPanel.add(courseTypeTextField);
		courseAddPanel.add(new JLabel("课程号"));
		courseAddPanel.add(courseNoTextField);
		courseAddPanel.add(new JLabel("课程名"));
		courseAddPanel.add(courseNameTextField);
		courseAddPanel.add(new JLabel("时间/地点"));
		courseAddPanel.add(courseDetailTextField);
		courseAddPanel.add(new JLabel("学分"));
		courseAddPanel.add(courseCreditTextField);
		courseAddPanel.add(new JLabel("授课老师编号"));
		courseAddPanel.add(teacherIdTextField);
		courseAddPanel.add(new JLabel("课程容量"));
		courseAddPanel.add(maxNumberTextField);
		courseAddPanel.add(new JLabel("已选人数"));
		courseAddPanel.add(selectNumberTextField);
		courseAddPanel.add(confirmButton);
		return courseAddPanel;
	}

	public CourseControllerListener getCourseControllerListener() {
		return courseControllerListener;
	}

	public void setCourseControllerListener(CourseControllerListener contactsControllerListener) {
		this.courseControllerListener = contactsControllerListener;
	}

	public void setCourseAddPanel(JPanel courseAddPanel) {
		this.courseAddPanel = courseAddPanel;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public JTextField getCourseIdTextField() {
		return courseIdTextField;
	}

	public void setCourseIdTextField(JTextField courseIdTextField) {
		this.courseIdTextField = courseIdTextField;
	}

	public JTextField getCourseTypeTextField() {
		return courseTypeTextField;
	}

	public void setCourseTypeTextField(JTextField courseTypeTextField) {
		this.courseTypeTextField = courseTypeTextField;
	}

	public JTextField getCourseNoTextField() {
		return courseNoTextField;
	}

	public void setCourseNoTextField(JTextField courseNoTextField) {
		this.courseNoTextField = courseNoTextField;
	}

	public JTextField getCourseNameTextField() {
		return courseNameTextField;
	}

	public void setCourseNameTextField(JTextField courseNameTextField) {
		this.courseNameTextField = courseNameTextField;
	}

	public JTextField getCourseDetailTextField() {
		return courseDetailTextField;
	}

	public void setCourseDetailTextField(JTextField courseDetailTextField) {
		this.courseDetailTextField = courseDetailTextField;
	}

	public JTextField getCourseCreditTextField() {
		return courseCreditTextField;
	}

	public void setCourseCreditTextField(JTextField courseCreditTextField) {
		this.courseCreditTextField = courseCreditTextField;
	}

	public JTextField getTeacherIdTextField() {
		return teacherIdTextField;
	}

	public void setTeacherIdTextField(JTextField teacherIdTextField) {
		this.teacherIdTextField = teacherIdTextField;
	}

	public JTextField getMaxNumberTextField() {
		return maxNumberTextField;
	}

	public void setMaxNumberTextField(JTextField maxNumberTextField) {
		this.maxNumberTextField = maxNumberTextField;
	}

	public JTextField getSelectNumberTextField() {
		return selectNumberTextField;
	}

	public void setSelectNumberTextField(JTextField selectNumberTextField) {
		this.selectNumberTextField = selectNumberTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

}
